package com.burgeon.backend.SHOP.controller;

import com.burgeon.backend.SHOP.entity.IndexCoupon;
import com.burgeon.backend.SHOP.service.AppCouponService;
import com.burgeon.backend.entity.Coupon;
import com.burgeon.backend.stsutil.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("shop/coupon")
public class AppCouponController extends BaseController {
    @Autowired
    private AppCouponService appCouponService;

    @RequestMapping("geterpdata")
    public ResponseResult<List<Coupon>> getErpCoupon(HttpSession session) {

        if (null == session.getAttribute("uid")) {
            return new ResponseResult<>(NOTLOG);
        }
        String phone = session.getAttribute("phone").toString();
        String token = session.getAttribute("token").toString();

        List<Coupon> data = appCouponService.getErpCoupon(phone, token);

        return new ResponseResult<List<Coupon>>(SUCCESS,data);
    }


    @RequestMapping("getindexcoupon")
    public ResponseResult<Void> createIndexCoupon(String cid, HttpSession session) {
        String uid = null;
        if (null != session.getAttribute("uid")) {
            uid = session.getAttribute("uid").toString();
        }else {
            return new ResponseResult<>(NOTLOG);
        }
        String phone = session.getAttribute("phone").toString();
        String token = session.getAttribute("token").toString();
        String openid = session.getAttribute("app_openid").toString();
        appCouponService.createIndexCoupon(cid, uid, phone, openid, token);
        return new ResponseResult<>(SUCCESS);
    }

    @RequestMapping("indexcoupon")
    public ResponseResult<List<IndexCoupon>> getIndexCoupon(HttpSession session) {
        String uid = null;
        if (null != session.getAttribute("uid")) {
            uid = session.getAttribute("uid").toString();
        }
        String token = session.getAttribute("token").toString();
        List<IndexCoupon> data = appCouponService.getIndexCoupon(uid, token);

        return new ResponseResult<List<IndexCoupon>>(SUCCESS, data);
    }

    @RequestMapping("data")
    public ResponseResult<List<Coupon>> getData(HttpSession session) {
        if (session.getAttribute("uid")==null){
            return new ResponseResult<>(NOTLOG);
        }
        String phone = session.getAttribute("phone").toString();
        String token = session.getAttribute("token").toString();
        List<Coupon> data = appCouponService.getVipShopCoupon(phone, token);

        return new ResponseResult<List<Coupon>>(SUCCESS, data);
    }

    @RequestMapping("usedata")
    public ResponseResult<List<Coupon>> getUseData(@RequestParam("price") String price,String[] gids, HttpSession session) {
        String phone = session.getAttribute("phone").toString();
        String token = session.getAttribute("token").toString();
        List<Coupon> data = appCouponService.getUserCoupon(gids,phone, price, token);
        System.err.println("phone=" + phone);
        System.err.println("price=" + price);
        return new ResponseResult<List<Coupon>>(SUCCESS, data);
    }

    /**
     * @author: 伯俊软件
     * @description: 优惠券详情
     * @date: 2021/9/8 9:28
     * @param session
     * @param code
     * @return com.burgeon.backend.stsutil.ResponseResult<com.burgeon.backend.entity.Coupon>
     */
    @RequestMapping("getCouponDetails")
    public ResponseResult<Coupon> getCouponDetails(HttpSession session,String code){
        String token = session.getAttribute("token").toString();
        Coupon data = appCouponService.getCouponDetails(token,code);
        return new ResponseResult<Coupon>(SUCCESS,data);
    }

}
